//package com.llb666.shop.gateway.filter;
//
//import com.llb666.common.core.utils.ServletUtils;
//import com.llb666.common.core.utils.text.AppStringUtils;
//import com.llb666.common.core.web.error.CommonError;
//import com.llb666.shop.gateway.client.AuthService;
//import com.llb666.shop.gateway.client.bo.Account;
//import com.llb666.shop.gateway.client.bo.AuthResponse;
//import com.llb666.shop.gateway.properties.WhiteProperties;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
///**
// * 权限过滤器：未登录用户不能访问用户中心接口.
// *  全局过滤器：所有接口都会经过此方法。 防止不必要的链路，后续改成局部过滤器
// *
// * @author llb666
// * @version 1.0
// */
//@Slf4j
//@Component
//public class GlobalAuthFilter implements GlobalFilter, Ordered {
//
//    /**
//     * 单体应用使用的header.
//     * String userId = request.getHeader("headerUserId");
//     * String userToken = request.getHeader("headerUserToken");
//     */
//    private static final String AUTH = "headerUserToken";
//    private static final String USER_ID = "headerUserId";
//
////    private static final String AUTH = "Authorization";
////    private static final String USER_ID = "shop-user-id";
//
//    private final AuthService authService;
//    private final WhiteProperties whiteProperties;
//
//    public GlobalAuthFilter(AuthService authService, WhiteProperties whiteProperties) {
//        this.authService = authService;
//        this.whiteProperties = whiteProperties;
//    }
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        log.info("auth start");
//        ServerHttpRequest request = exchange.getRequest();
//        String url = request.getURI().getPath();
//        // 跳过不需要验证的路径,如果不在该名单中，不需要验证！
//        if (!AppStringUtils.matches(url, whiteProperties.getWhites())) {
//            return chain.filter(exchange);
//        }
//        HttpHeaders headers = request.getHeaders();
//        String token = headers.getFirst(AUTH);
//        String userId = headers.getFirst(USER_ID);
//
//        ServerHttpResponse response = exchange.getResponse();
//        if (StringUtils.isBlank(token)) {
//            log.error("token不存在！");
//            response.setStatusCode(HttpStatus.UNAUTHORIZED);
//            return unauthorizedResponse(exchange, "令牌不能为空");
//        }
//        //  后续需要修改登录接口，按照auth/token的方式获取token,此处方能校验.
//        Account account = Account.builder().userId(userId).token(token).build();
//        AuthResponse verify = authService.verify(account);
//        if (verify.getCode() != 1L) {
//            log.error("token不存在！");
//            response.setStatusCode(HttpStatus.FORBIDDEN);
//            return unauthorizedResponse(exchange, "令牌检验失败");
//        }
//        // 用户信息存入header
//        ServerHttpRequest.Builder mutate = request.mutate();
//        mutate.header(USER_ID, userId);
//        mutate.header(AUTH, token);
//        ServerHttpRequest buildRequest = mutate.build();
//
//        response.setStatusCode(HttpStatus.OK);
//        response.getHeaders().add(USER_ID, userId);
//        response.getHeaders().add(AUTH, token);
//        return chain.filter(exchange.mutate()
//                .request(buildRequest)
//                .response(response)
//                .build());
//    }
//
//    @Override
//    public int getOrder() {
//        return 0;
//    }
//
//    private Mono<Void> unauthorizedResponse(ServerWebExchange exchange, String msg) {
//        log.error("[鉴权异常处理]请求路径:{}", exchange.getRequest().getPath());
//        return ServletUtils.webFluxResponseWriter(exchange.getResponse(), msg, CommonError.NeedAuthentication);
//    }
//}
